package com.cash.app.ui.main.fg.home

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.cash.app.base.BaseViewModel
import com.cash.app.manager.api.GlobalAPIService
import com.cash.app.manager.model.FastApplyResponse
import com.cash.app.manager.model.ProductIdRequest
import com.cash.app.manager.model.RealProductResponse
import com.cash.app.net.NetRequestExecutor
import com.cash.app.net.RetrofitClient
import com.cash.app.ui.main.fg.home.api.HomeAPIService
import com.cash.app.ui.main.fg.home.model.HomeResponse

class HomeViewModel : BaseViewModel() {

    private val apiService by lazy {
        RetrofitClient.createService(HomeAPIService::class.java)
    }
    private val globalAPIService by lazy {
        RetrofitClient.createService(GlobalAPIService::class.java)
    }

    private val _products = MutableLiveData<HomeResponse>()
    val products: LiveData<HomeResponse> = _products

    private val _realProductData = MutableLiveData<RealProductResponse>()
    val realProductData: LiveData<RealProductResponse> = _realProductData

    private val _fastApplyData = MutableLiveData<FastApplyResponse>()
    val fastApplyData: LiveData<FastApplyResponse> = _fastApplyData

    fun fetchHomeData() {
        launchRequest(
            request = { NetRequestExecutor.baseNetRequest { apiService.getHomeData() } },
            onStart = { },
            onSuccess = { response ->
                response?.let {
                    _products.value = it
                }
            })
    }

    fun showRealProduct(productId: Int) {
        launchRequest(request = {
            NetRequestExecutor.baseNetRequest {
                globalAPIService.showRealProduct(ProductIdRequest(productId))
            }
        }, onSuccess = { response ->
            response?.let {
                _realProductData.value = it
            }
        })
    }

    fun fastApply(productId: Int) {
        launchRequest(request = {
            NetRequestExecutor.baseNetRequest {
                globalAPIService.fastApply(ProductIdRequest(productId))
            }
        }, onSuccess = { response ->
            response?.let {
                _fastApplyData.value = it
            }
        })
    }

//    fun fetchUser2(userId: Long, userId2: Long) {
//        viewModelScope.launch {
//            val deferredResult = async(Dispatchers.IO) {
//                NetRequestExecutor.baseNetRequest { apiService.getUser(userId) }
//            }
//            val deferredResult2 = async(Dispatchers.IO) {
//                NetRequestExecutor.baseNetRequest { apiService.getUser(userId2) }
//            }
//
//            try {
//                val results = awaitAll(deferredResult, deferredResult2)
//
//                when {
//                    results[0] is NetResult.Success && results[1] is NetResult.Success -> {
//                        // viewModel handle
//                    }
//                    else -> {
//                        // handleError
//                    }
//                }
//            } catch (e: Exception) {
//                TLog.e("fetchUser2 Exception: $e")
//            }
//        }
//    }
}